package rpg.writer.scripts.impl;

import rpg.client.model.ActorInfo;
import rpg.engine.client.GameObjectInfo;
import rpg.engine.model.DialogInfo;
import rpg.engine.model.GameEffect;
import rpg.engine.model.GameMap;
import rpg.engine.model.MagicArea;
import rpg.engine.model.Story;
import rpg.engine.model.World;
import rpg.util.Objects;

public final class WorldTool {
	private final ParsedScript script;
	
	private WorldTool(ParsedScript script)  {
		super();
		this.script = script;
	}
	
	public static World build(ParsedScript script)  {
		Objects.requireNonNull(script, "WorldTool.build(script)");
		WorldTool wt = new WorldTool(script);
		return wt.go();
	}

	private World go() {
		String firstMapID = "";
		final World.Builder wb = new World.Builder();
		
		for (GameMap gm : script.getGameMaps())  {
			if (firstMapID.length() == 0)  {
				firstMapID = gm.getId();
			}
			wb.gameMap(gm);
		}
		
		final String STORY_ID = script.getStoryID();
		
		Story.Builder sb = new Story.Builder();
		sb.id(STORY_ID);
		
		for (GameEffect i : script.getStartEffects())  {
			sb.effect(i);
		}

		// Add the Dialog
		for (DialogInfo di : script.getDialogs())  {
			sb.chat(di);
		}
		
		// Add the Magic Area
		for (MagicArea ma : script.getMagicAreas())  {
			sb.magicArea(ma);
		}
		
		Story s = sb.build();
		
		wb.story(s);
		
		wb.startStoryID(s.getId());
		
		// Add the NPCs...
		for (ActorInfo a : script.getActors())  {
			wb.npc(a);
		}

		// Add the Objects
		for (GameObjectInfo go : script.getObjects())  {
			wb.gameObject(go);
		}
		
		return wb.build();
	}
}
